﻿using MebAppMarketplace.Biz;
using MebAppMarketplace.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MebAppMarketplace.Controllers
{
    /// <summary>
    /// 管理功能，用于后台人员登录和显示后台操作链接
    /// </summary>
    [Authorize(Roles = "Admin")]
    public class AdministrationController : Controller
    {
        public UserService userService { get; set; }
        
        /// <summary>
        /// 显示后台操作界面
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }
        [AllowAnonymous]
        public IActionResult Login()
        {
            return View();
        }
        [AllowAnonymous]
        [HttpPost]
        public async Task<IActionResult> Login(string username, string password)
        {
            var result = await userService.LoginUser(username, password);
            if (result == Model.LoginResult.登录成功)
            {
                return Redirect("/administration");
            }
            ModelState.AddModelError("", result.ToString());
            return this.ViewError();
        }
    }
}
